package cn.liweixi._03_oneway;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class consumer {
    public static void main(String[] args) throws Exception {
        //1. 创建consumer对象
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("heiheihei-oneway");
        //2. 设置Name Server地址
        consumer.setNamesrvAddr("127.0.0.1:9876");
        //3. 设置消费位置，从最开始销毁
        consumer.subscribe("topic-oneway","tag-oneway");
        //4. 设置消息回调处理监听 -> 处理消息
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> exts, ConsumeConcurrentlyContext context) {
                try {
                    for (MessageExt ext : exts) {
                        System.out.println(new String(ext.getBody()));
                        System.out.println(context);
                    }
                    //标记已经被消费成功
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                } catch (Exception e) {
                    e.printStackTrace();
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
            }
        });
        //5. Start consumer
        consumer.start();
    }
}
